﻿Namespace DoFactory.GangOfFour.Memento.Structural

    ''' <summary>
    ''' MainApp startup class for Structural
    ''' Memento Design Pattern.
    ''' </summary>
    Public Class MainApp

        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()

            Dim o As New Originator()
            o.State = "On"

            ' Store internal state
            Dim c As New Caretaker()
            c.Memento = o.CreateMemento()

            ' Continue changing originator
            o.State = "Off"

            ' Restore saved state
            o.SetMemento(c.Memento)

            ' Wait for user
            ''Console.ReadKey()
        End Sub

    End Class

    ''' <summary>
    ''' The 'Originator' class
    ''' </summary>
    Public Class Originator

        Private _state As String

        ' Property
        Public Property State() As String
            Get
                Return _state
            End Get
            Set(ByVal value As String)
                _state = value
                Console.WriteLine("State = " & _state)
            End Set
        End Property

        ' Creates memento
        Public Function CreateMemento() As Memento
            Return (New Memento(_state))
        End Function

        ' Restores original state
        Public Sub SetMemento(ByVal memento As Memento)
            Console.WriteLine("Restoring state...")
            State = memento.State
        End Sub

    End Class



    ''' <summary>
    ''' The 'Memento' class
    ''' </summary>
    Public Class Memento

        Private _state As String

        ' Constructor
        Public Sub New(ByVal state As String)
            Me._state = state
        End Sub

        ' Gets or sets state
        Public ReadOnly Property State() As String
            Get
                Return _state
            End Get
        End Property

    End Class

    ''' <summary>
    ''' The 'Caretaker' class
    ''' </summary>
    Public Class Caretaker

        Private _memento As Memento

        ' Gets or sets memento
        Public Property Memento() As Memento
            Get
                Return _memento
            End Get
            Set(ByVal value As Memento)
                _memento = value
            End Set
        End Property


    End Class

End Namespace
